package com.alibaba._03XHR;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import java.nio.charset.StandardCharsets;

public class producter {

    public static void main(String[] args) throws Exception {
        // 1.创建生产者对象
        DefaultMQProducer producer = new DefaultMQProducer("producer_test03_group");

        // 2.连接mq
        producer.setNamesrvAddr("127.0.0.1:9876");

        // 3. 生产启动
        producer.start();

        // 4.发送消息
        // 4.1创建主体
        Message message = new Message("test03_topic", "test03_tags", "异步消息你接受到了吗".getBytes(StandardCharsets.UTF_8));

        // 4.2发送
        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable);
            }
        });

        // 4.3打印发送结果
        System.out.println("异步请求");
        // 4.4异步不能关闭，如果还没发生异步请求就被关了就不会在发生了
//        producer.shutdown();

    }
}
